package com.qf.future;

import com.utils.ExecutorUtils;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 任务穿行
 */
public class Demo02 {

    // 一个线程做计算 10+10
    // 另一个线程从线程1的返回结果之上*20
    public static void main(String[] args) throws ExecutionException, InterruptedException {

        ThreadPoolExecutor pool = ExecutorUtils.getExcutorsPool();
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + ",开始计算。。");
            return 10 + 20;
        }, pool)
//                .thenRunAsync(() -> { // 只要上面的任务执行完成后开始执行
//            System.out.println(Thread.currentThread().getName() + ",计算完成。。");
//        }, pool);

//                .thenAcceptAsync((resp) -> { // 上面一个线程执行完成后调用，可以获取到结算结果
//                    System.out.println(Thread.currentThread().getName() + ",计算完成。resp:"+resp);
//                }, pool);

                .thenApplyAsync((resp) -> {
                    System.out.println(Thread.currentThread().getName() + ",计算完成。resp:" + resp);
                    return 20 * resp;
                }, pool);


        System.out.println(Thread.currentThread().getName() + ",sum:" + future.get());
        pool.shutdown();

    }
}
